<template>
	<view class="login-container">
		<view class="login-card">
			<view class="title-container">
				<view class="title-line"></view>
				<text class="title-text">登录</text>
			</view>

			<!-- 表单 -->
			<uv-form class="form" :rules="rules" :model="form" ref="formRef" label-position="top">
				<view class="input-container" :class="{ 'error': studentIdError }">
					<view class="input-icon">
						<icon class="ic-my iconfont bs-my" type=""></icon>
					</view>
					<uv-form-item prop="studentId" class="form-item">
						<uv-input class="login-input" :placeholder="studentIdError ? '请输入学号' : '请输入学号'"
							:placeholder-style="studentIdError ? 'color: #ff4d4f;' : ''" border="none"
							v-model="form.studentId" @blur="validateStudentId" confirm-type="next"
							@confirm="focusPassword">
						</uv-input>
					</uv-form-item>
				</view>
				<view class="input-container" :class="{ 'error': passwordError }">
					<view class="input-icon">
						<icon class="ic-password iconfont bs-password" type=""></icon>
					</view>
					<uv-form-item prop="password" class="form-item">
						<uv-input class="login-input" :placeholder="passwordError ? '请输入密码' : '请输入密码'"
							:placeholder-style="passwordError ? 'color: #ff4d4f;' : ''" border="none" type="password"
							v-model="form.password" ref="passwordInput" @blur="validatePassword" />
					</uv-form-item>
				</view>
			</uv-form>

			<!-- uv-toast组件 -->
			<uv-toast ref="toast" />

			<!-- 登录按钮 -->
			<view class="btn-container">
				<uv-button type="primary" size="large" text="登录" @click="handleLogin" shape="circle" class="login-btn"
					hover-class="btn-hover"></uv-button>
			</view>

			<view class="switch-mode" @click="goToSign">
				<text class="left">还没有账号?</text>
				<text class="right">马上注册</text>
			</view>
		</view>

		<!-- 背景动画元素 -->
		<view class="circle circle-1"></view>
		<view class="circle circle-2"></view>
		<view class="circle circle-3"></view>
	</view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import db from '@/api/database'

const toast = ref()
const router = useRouter()
const passwordInput = ref(null)

const form = reactive({
	studentId: '',
	password: ''
})

const formRef = ref(null)
const studentIdError = ref(false)
const passwordError = ref(false)

const validateStudentId = () => {
	if (!form.studentId) {
		studentIdError.value = true
	} else {
		studentIdError.value = false
	}
}

const validatePassword = () => {
	if (!form.password) {
		passwordError.value = true
	} else {
		passwordError.value = false
	}
}

const focusPassword = () => {
	passwordInput.value?.focus()
}

const rules = {
	studentId: [
		{
			required: true,
			validator: (rule, value, callback) => {
				studentIdError.value = !value
				callback()
			}
		}
	],
	password: [
		{
			required: true,
			validator: (rule, value, callback) => {
				passwordError.value = !value
				callback()
			}
		}
	]
}

// 页面加载时检查登录状态
onMounted(() => {
	const user = uni.getStorageSync('user')
	if (user) {
		// 如果已登录，跳转到首页
		router.push('/pages/index/index')
	}
})

// 登录操作
function handleLogin() {
	validateStudentId()
	validatePassword()

	if (!form.studentId || !form.password) {
		return
	}

	formRef.value?.validateField(['studentId', 'password'], async (valid) => {
		if (valid && valid.length === 0) {
			try {
				const res = await db.collection('grxx').where({ xh: form.studentId }).get()
				if (res.data.length === 0) {
					toast.value?.show({ message: '账号未注册', type: 'error' })
					setTimeout(() => {
						router.push('/pages/sign/sign') // 跳转到注册页面
					}, 1000)
				} else {
					if (res.data[0].mm !== form.password) {
						toast.value?.show({ message: '密码错误', type: 'error' })
					} else {
						toast.value?.show({ message: '登录成功', type: 'success' })
						uni.setStorageSync('user', res.data[0])
						setTimeout(() => {
							router.push('/pages/index/index') // 跳转到首页
						}, 1000)
					}
				}
			} catch (err) {
				toast.value?.show({ message: '登录失败，请稍后重试', type: 'error' })
			}
		}
	})
}

// 跳转到注册页面
function goToSign() {
	router.push('/pages/sign/sign') // 跳转到注册页面
}
</script>

<style lang="scss" scoped>
@import "/static/css/iconfont.css";

/* 关键帧动画定义 */
@keyframes float {
	0% {
		transform: translateY(0px);
	}

	50% {
		transform: translateY(-20px);
	}

	100% {
		transform: translateY(0px);
	}
}

@keyframes pulse {
	0% {
		transform: scale(1);
		opacity: 0.8;
	}

	50% {
		transform: scale(1.05);
		opacity: 0.6;
	}

	100% {
		transform: scale(1);
		opacity: 0.8;
	}
}

@keyframes fadeIn {
	from {
		opacity: 0;
		transform: translateY(20px);
	}

	to {
		opacity: 1;
		transform: translateY(0);
	}
}

.login-container {
	width: 100%;
	height: 100vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
	overflow: hidden;
	position: relative;
}

.login-card {
	width: 92%;
	max-width: 600rpx;
	background-color: rgba(255, 255, 255, 0.9);
	border-radius: 20px;
	padding: 40rpx 30rpx;
	box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
	z-index: 10;
	animation: fadeIn 0.8s ease-out;
	position: relative;
}

.title-container {
	display: flex;
	flex-direction: column;
	align-items: center;
	margin-bottom: 40px;
	position: relative;
}

.title-line {
	width: 60px;
	height: 4px;
	background: linear-gradient(90deg, #22c4ff, #2196f3);
	border-radius: 2px;
	margin-bottom: 15px;
	transition: width 0.3s ease;
}

.title-text {
	font-size: 40rpx;
	font-weight: bold;
	color: #333;
	position: relative;

	&::after {
		content: '';
		position: absolute;
		bottom: -8px;
		left: 0;
		width: 100%;
		height: 3px;
		background: linear-gradient(90deg, #22c4ff, #2196f3);
		transform: scaleX(0);
		transition: transform 0.3s ease;
	}

	&:active::after {
		transform: scaleX(1);
	}
}

.form {
	width: 90%;
	margin: 0 auto;
}

.input-container {
	display: flex;
	align-items: center;
	background-color: #f5f7fa;
	border-radius: 50rpx;
	padding: 10rpx 0;
	margin-bottom: 30rpx;
	transition: all 0.3s ease;
	box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
	border: 1px solid rgba(255, 255, 255, 0.5);

	&:active {
		box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
	}

	&:focus-within {
		box-shadow: 0 5px 15px rgba(34, 196, 255, 0.2);
		border: 1px solid rgba(34, 196, 255, 0.5);
	}

	&.error {
		border: 1px solid #ff4d4f;
		box-shadow: 0 2px 10px rgba(255, 77, 79, 0.1);
	}
}

.input-icon {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 60rpx;
	height: 60rpx;
	color: #22c4ff;
	margin-left: 10px;

	.ic-my {
		font-size: 40rpx;
	}

	.ic-password {
		font-size: 40rpx;
	}
}

.form-item {
	flex: 1;
}

.login-input {
	font-size: 30rpx;
}

.btn-container {
	margin: 50rpx auto 30rpx;
	width: 85%;
}

.login-btn {
	background: linear-gradient(45deg, #22c4ff, #2196f3) !important;
	box-shadow: 0 10px 20px rgba(34, 196, 255, 0.3);
	transition: all 0.3s ease !important;
	border: none !important;
	border-radius: 50rpx !important;
	// height: 90rpx;
	font-size: 32rpx;
	font-weight: bold;
	letter-spacing: 4rpx;
}

.btn-hover {
	transform: translateY(-3px);
	box-shadow: 0 15px 25px rgba(34, 196, 255, 0.4);
	opacity: 0.9;
}

.switch-mode {
	z-index: 1;
	font-size: 28rpx;
	margin-top: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: all 0.3s ease;
	padding: 20rpx 0;

	.left {
		color: #8a9099;
	}

	.right {
		color: #22c4ff;
		margin-left: 10rpx;
		font-weight: bold;
		transition: all 0.3s ease;

		&:active {
			color: #2196f3;
			transform: scale(1.05);
		}
	}

	&:active {
		transform: translateY(-2px);
	}
}

/* 背景动画圆圈 */
.circle {
	position: absolute;
	border-radius: 50%;
	background: linear-gradient(45deg, rgba(34, 196, 255, 0.2), rgba(33, 150, 243, 0.2));
	filter: blur(5px);
}

.circle-1 {
	width: 500rpx;
	height: 500rpx;
	top: -150rpx;
	right: -100rpx;
	animation: pulse 8s infinite ease-in-out;
}

.circle-2 {
	width: 350rpx;
	height: 350rpx;
	bottom: 100rpx;
	left: -100rpx;
	animation: pulse 6s infinite ease-in-out 1s;
}

.circle-3 {
	width: 600rpx;
	height: 600rpx;
	bottom: -300rpx;
	right: 30%;
	animation: pulse 10s infinite ease-in-out 2s;
}
</style>
